package com.uff.hmstpa.vision.impl;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import com.uff.hmstpa.controller.ScheduleSystemController;
import com.uff.hmstpa.controller.impl.ScheduleSystemControllerImpl;
import com.uff.hmstpa.model.ScheduleSystemModel;
import com.uff.hmstpa.model.domain.Appointment;
import com.uff.hmstpa.model.domain.Doctor;
import com.uff.hmstpa.model.domain.Healthplan;
import com.uff.hmstpa.model.domain.Patient;
import com.uff.hmstpa.util.command.Command;
import com.uff.hmstpa.util.observer.Observer;
import com.uff.hmstpa.util.observer.Subject;
import com.uff.hmstpa.vision.ScheduleSystemView;
import com.uff.hmstpa.vision.command.SelectDoctorCommand;
import com.uff.hmstpa.vision.impl.windows.CreateAppointmentFrame;
import com.uff.hmstpa.vision.impl.windows.CreatePatientFrame;

public class ScheduleSystemViewUI implements ScheduleSystemView, Observer {

	private JFrame frmAppointmentScheduleSystem;

	private CreateAppointmentFrame createAppFrame;
	private CreatePatientFrame createPatientFrame;

	JList<Appointment> jList_SelectedApptsList = new JList<Appointment>();
	JMenu mnDoutores = null;

	private ScheduleSystemController controller;
	private ScheduleSystemModel model;
	private JTextField textField_SelectedDoctorName;
	private JTextField textField_SelectedDoctorCRM;

	public ScheduleSystemViewUI(ScheduleSystemModel model) {
		this.controller = ScheduleSystemControllerImpl.instance();
		this.model = model;
		initialize();
	}

	private void initialize() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {}

		frmAppointmentScheduleSystem = new JFrame();

		frmAppointmentScheduleSystem.setTitle("Sistema de Cadastro e Consulta");
		frmAppointmentScheduleSystem.setBounds(100, 100, 500, 500);
		frmAppointmentScheduleSystem.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmAppointmentScheduleSystem.getContentPane().setLayout(null);
		frmAppointmentScheduleSystem.setLocationRelativeTo(null);

		JPanel panel = new JPanel();
		panel.setBounds(0, 0, 484, 64);
		frmAppointmentScheduleSystem.getContentPane().add(panel);
		panel.setLayout(null);

		JLabel lblDoutor = new JLabel("Doutor:");
		lblDoutor.setBounds(10, 11, 37, 14);
		panel.add(lblDoutor);

		textField_SelectedDoctorName = new JTextField();
		textField_SelectedDoctorName.setEditable(false);
		textField_SelectedDoctorName.setBounds(66, 8, 408, 20);
		panel.add(textField_SelectedDoctorName);
		textField_SelectedDoctorName.setColumns(10);

		JLabel lblCrm = new JLabel("CRM:");
		lblCrm.setBounds(20, 38, 26, 14);
		panel.add(lblCrm);

		textField_SelectedDoctorCRM = new JTextField();
		textField_SelectedDoctorCRM.setEditable(false);
		textField_SelectedDoctorCRM.setBounds(66, 35, 408, 20);
		panel.add(textField_SelectedDoctorCRM);
		textField_SelectedDoctorCRM.setColumns(10);

		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Pr\u00F3ximas Consultas", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_2.setBounds(0, 75, 484, 373);
		frmAppointmentScheduleSystem.getContentPane().add(panel_2);
		panel_2.setLayout(null);
		jList_SelectedApptsList.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				Appointment app = jList_SelectedApptsList.getSelectedValue();
				if(app != null) {
					if(e.getButton() == MouseEvent.BUTTON3) {
						int selected = JOptionPane.showConfirmDialog(null, "Deseja realmente deletar a consulta " + jList_SelectedApptsList.getSelectedValue().toString() + " ?");
						if(selected == JOptionPane.OK_OPTION) {
							controller.deleteAppointment(app);
						}						
					}

					if(e.getClickCount() >= 2) {
						System.out.println("EDIT: " + app.toString());
						//TODO chama janela de edição de appointment
					}
				}
			}
		});

		jList_SelectedApptsList.setBorder(new LineBorder(Color.LIGHT_GRAY));
		jList_SelectedApptsList.setBounds(10, 21, 464, 341);
		jList_SelectedApptsList.setModel(new DefaultListModel<Appointment>());
		panel_2.add(jList_SelectedApptsList);

		JMenuBar menuBar = new JMenuBar();
		frmAppointmentScheduleSystem.setJMenuBar(menuBar);

		mnDoutores = new JMenu("Doutores");
		menuBar.add(mnDoutores);

		JMenu mnPacientes = new JMenu("Pacientes");
		menuBar.add(mnPacientes);

		JMenuItem mntmCadastrarPaciente = new JMenuItem("Cadastrar Paciente");
		mntmCadastrarPaciente.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				createPatientFrame.setVisible(true);
			}
		});
		mnPacientes.add(mntmCadastrarPaciente);

		JMenuItem mntmEditarPaciente = new JMenuItem("Editar Paciente");
		mntmEditarPaciente.setEnabled(false);
		mnPacientes.add(mntmEditarPaciente);

		JMenuItem mntmRemoverPaciente = new JMenuItem("Remover Paciente");
		mntmRemoverPaciente.setEnabled(false);
		mnPacientes.add(mntmRemoverPaciente);

		JMenu mnConsultas = new JMenu("Consultas");
		menuBar.add(mnConsultas);

		JMenuItem mntmNovaConsulta = new JMenuItem("Nova Consulta");
		mntmNovaConsulta.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				createAppFrame.setVisible(true);
			}
		});
		mnConsultas.add(mntmNovaConsulta);

		JMenuItem mntmEditarConsulta = new JMenuItem("Editar Consulta");
		mntmEditarConsulta.setEnabled(false);
		mnConsultas.add(mntmEditarConsulta);

		JMenuItem mntmCancelarConsulta = new JMenuItem("Cancelar Consulta");
		mntmCancelarConsulta.setEnabled(false);
		mnConsultas.add(mntmCancelarConsulta);

		frmAppointmentScheduleSystem.setVisible(true);

		createAppFrame = new CreateAppointmentFrame(model);
		createAppFrame.setLocationRelativeTo(null);
		createPatientFrame = new CreatePatientFrame(model);
		createPatientFrame.setLocationRelativeTo(null);
	}

	public void updateAppointmentList() {
		jList_SelectedApptsList.setListData(model.getAppointments().toArray(new Appointment[]{}));
		Doctor d = model.getSelectedDoctor();
		if(d != null) {
			this.textField_SelectedDoctorName.setText(d.getName() + " (" + d.getSpecialty() + ")");
			this.textField_SelectedDoctorCRM.setText(d.getCRM());
		} else {
			this.textField_SelectedDoctorName.setText("Todos");
			this.textField_SelectedDoctorCRM.setText("N/A");
		}
	}

	public void updateDoctorsList() {
		SelectDoctorCommand selectDoctorCommand = new SelectDoctorCommand();

		JMenuItem mntmTodos = new JMenuItem(selectDoctorCommand);
		mntmTodos.setText("Todos");
		mnDoutores.add(mntmTodos);

		createAppFrame.doctorComboBox.removeAllItems();
		
		for(Doctor d : model.getDoctors()) {
			JMenuItem mntmDoctor = new JMenuItem(selectDoctorCommand);
			mntmDoctor.setText(d.getName());
			mnDoutores.add(mntmDoctor);

			//Atualização da lista de doutores da janela auxiliar
			createAppFrame.doctorComboBox.addItem(d);
		}

	}

	public void updatePatientsList() {
		createAppFrame.patientComboBox.removeAllItems();
		
		List<Patient> patients = model.getPatients();

		for(Patient p : patients) {
			createAppFrame.patientComboBox.addItem(p);
		}
	}

	@Override
	public void updateHealthplanComboBox() {
		this.createPatientFrame.healthPlanComboBox.removeAllItems();
		
		for(Healthplan hp : model.getHealthplans()) {
			this.createPatientFrame.healthPlanComboBox.addItem(hp);
		}

	}

	public void update(Subject subject, Object arg) {
		if(arg instanceof Command) {
			((Command) arg).execute();
		}
	}

}
